import {Tools} from 'weaver-mobile';
let emClient = null;
const getLabel = Tools.getLabel;

//初始化EM-SDK对象
export const doInitEmSDK = () => {
    if (!window.em) 
        return;
    emClient = window.em;
}

export {emClient}

//判断是否是否支持EM接口
const judgeApiSupport = (apiname) => {
    return !!emClient;
}

//重置EM表单的头
export const resetEmElement = () => {
    if (judgeApiSupport("resetNavigation")) {
        emClient.ready(function(){
            emClient.resetNavigation({title: ''});
        });
    }
}

//定制EM表单的头
export const setEmTopNav = (pathname) => {
    let topTitle = "";
    let topShowCloseButton = true;
    if(pathname === "/add"){
        topTitle = getLabel(16392, "新建流程");
    }else if (pathname == "/center/doing/qysauth") {
        topTitle = getLabel(501195, "身份认证");
    }/**else if(pathname.includes("/center/doneFinish")){
        topTitle = getLabel(508220,"办结");
    }else if(pathname.includes("/center/flowCS")){
        topTitle = getLabel(2084,"抄送")
    }**/else if(pathname.includes("/monitor/noPermission")){
        topTitle = getLabel(32210,"流程信息");
    }/**else if(pathname.includes("/center/doing")){
        topTitle = getLabel(500431, '待办事宜');
    }else if(pathname.includes("/center/done")){
        topTitle = getLabel(17991, '已办事宜');
    }else if(pathname.includes("/center/mine")){
        topTitle =  getLabel(1210, '我的请求');
    }else if(pathname.includes("/center/all")){
        topTitle =  getLabel(84382, '全部流程');
    }else if(pathname.includes("/center/attentionAll")){
        topTitle =  getLabel(503951, '关注事宜');
    }**/
    const routeMapCloseButton = {
        "/add": true,
        "/monitor/noPermission": false,
        "/center/doing/qysauth": false
    }
    topShowCloseButton = routeMapCloseButton[pathname];    
    controlEmTopNav({ title: topTitle, showCloseButton: topShowCloseButton});
}

/**
 * 控制EM客户端顶部头信息
 */
export const controlEmTopNav = (params = {}) => {
    document.title = params.title ?  params.title : "" ;//和飞信中走此段,和飞信不进下面if语句
    if (!judgeApiSupport("configNavigation")){
        return;
    }
    const nav = {
        'success': function (argument) {
            console.log('controlEmTopNav success', JSON.stringify(argument));
        },
        'fail': function (argument) {
            console.log('controlEmTopNav fail', JSON.stringify(argument));
        },
        'left': {},
        'title': '',
        showCloseButton: true
    };
   
    emClient.ready(function(){
        if(params.title===undefined){
            params.title = "";
        }
        emClient.configNavigation({...nav, ...params});//微信中走此段
    });
}